<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/9/29
 * Time: 12:16
 */

namespace app\admin\controller;


use app\common\model\MenuModel;
use app\common\model\RoleMenuModel;
use think\Exception;
use think\Request;
use think\facade\Db;
class MenuController  extends Base
{
    private $model;

    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new MenuModel();
    }
    //根据权限获取左侧一二级菜单
    public function getMenuListByRoleLeft()
    {
        $role_id    = $this->role_ids;

        $rst = $this->model->getMenuLeft($role_id);
        $this->result($rst, '操作成功', 1);

    }
    /**
     * 在分配页面权限获取 全部菜单
     * @param \app\adminapi\model\SysMenu $sysMenu
     */
    public function getMenuListByRole(Request $request )
    {
        $role_id = $request->param('role_id');
        $s = new RoleMenuModel();
        $arr = $s->where('role_id',$role_id)->select()->toArray();
        $menu_arr = array_column($arr,'menu_id');
        $field   = 'id,no,parent_id,level,order,name,link,icon,status,path,hidden,href,update_time,perms,menuType';
        $allMenu = MenuModel::field($field)->select()->toArray();

        foreach($allMenu as &$v){
            $v['is_check'] = 0;
            if(in_array($v['id'],$menu_arr)){
                $v['is_check'] = 1;
            }
        }
       // $rst     = $this->model->getMenu($role_id);
        $this->result($allMenu, '操作成功', 1);

    }
    /**
     * 菜单列表
     */
    public function getMenuList(Request $request)
    {

        $field   = 'id,no,parent_id,level,order,name,link,icon,status,path,hidden,href,update_time,perms,menuType';

        $allMenu = MenuModel::field($field)->select();
        $menu = [];
        foreach ($allMenu as $key => $val) {
            $data['searchValue'] = null;
            $data['createBy']    = null;
            $data['createTime']  = null;
            $data['updateBy']    = null;
            $data['remark']      = null;
            $data['parentName']  = null;
            $data['children']    = [];
            $data['params']      = (object)[];
            $data['isFrame']     = 1;
            $data['updateTime']  = $val->update_time;
            $data['menuId']      = $val->id;
            $data['menuName']    = $val->name;
            $data['parentId']    = intval($val->parent_id);
            $data['orderNum']    = $val->order;
            $data['path']        = $val->path;
            $data['component']   = $val->link;
            $data['menuType']    = $val->menuType;
            $data['visible']     = $val->hidden;
            $data['status']      = $val->status;
            $data['perms']       = $val->perms;
            $data['icon']        = $val->icon;
            array_push($menu, $data);
        }
        $this->result($menu, '操作成功', 1);
    }
    /**
     * 给角色添加权限
     * @param \think\Request                      $request
     * @param \app\adminapi\validate\MenuValidate $validate
     * @throws \Exception
     */
    public function addMenuToRole(Request $request)
    {
        try {
            $menuIds = $request->param('menu_ids'); #menu 菜单1,2,3,4
            $role_id = $request->param('role_id');
            $arr     = explode(',', $menuIds);
            $m       = [];
            Db::startTrans();
            # 先删除所有权限
            $d = RoleMenuModel::where('role_id', $role_id)->findOrEmpty();
            if (!$d->isEmpty()) {
                $delete = RoleMenuModel::where('role_id', $role_id)->delete();
            } else {
                $delete = true;
            }

            foreach ($arr as $key => $val) {
                $m[$key]['role_id'] = $role_id;
                $m[$key]['menu_id'] = $val;
            }

            # 添加所有权限
            $s = RoleMenuModel::insertAll($m);

            if ($delete && $s) {
                Db::commit();
                $this->result([], '授权成功', 1);
            } else {
                Db::rollback();
                $this->result([], '授权失败', []);

            }
        } catch (Exception $e) {
            Db::rollback();
            $this->result([], $e->getMessage(), []);
        }
    }

}